package com.duoduo.admin.controller;

import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.duoduo.common.core.constant.CacheConstants;
import com.duoduo.common.core.util.Page;
import com.duoduo.common.core.util.PageRequest;
import com.duoduo.common.core.util.R;
import com.mason.duoduo.log.annotation.SysLog;
import com.duoduo.admin.api.dto.SysDictDTO;
import com.duoduo.admin.entity.SysDictItem;
import com.duoduo.admin.service.SysDictService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("sysDict")
@Tag(name = "字典表接口", description ="字典表访问接口")
@RequiredArgsConstructor
public class SysDictController {
	
	
	private final SysDictService sysDictService;
	
	/**
	 * 分页
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Operation(summary = "分页")
	public R<Page<SysDictDTO>> page(PageRequest<SysDictDTO> request){
		Page<SysDictDTO> page = sysDictService.page(request.getPage(),request.getPageSize(),request.getParam());
		return R.ok(page);
	}
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@GetMapping("{id}/get")
	@Operation(summary = "详情")
	public R<SysDictDTO> get(@Parameter(description = "主键id") @PathVariable("id")Long id){
		return R.ok(sysDictService.get(id));
	}
	/**
	 * 创建
	 * @param id
	 * @return
	 */
	@Operation(summary = "创建")
	@PostMapping("create")
	public R<SysDictDTO> create(SysDictDTO request){
		sysDictService.create(request);
		return R.ok();
	}
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@Operation(summary = "修改")
	@PutMapping("update")
	@SysLog("修改字典")
	@PreAuthorize("@pms.hasPermission('sys_dict_edit')")
	public R<SysDictDTO> update(SysDictDTO request){
		sysDictService.updateDict(request);
		return R.ok();
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除")
	@DeleteMapping("{id}/delete")
	public R<SysDictDTO> delete(@Parameter(description = "主键id") @PathVariable("id")Long id){
		sysDictService.removeDict(id);
		return R.ok();
	}
	
	@SysLog("清除字典缓存")
	@DeleteMapping("/cache")
	@PreAuthorize("@pms.hasPermission('sys_dict_del')")
	public R<Void> clearDictCache() {
		sysDictService.clearDictCache();
		return R.ok();
	}
	/**
	 * 通过字典类型查找字典
	 * @param type 类型
	 * @return 同类型字典
	 */
	@GetMapping("/key/{key}")
	@Cacheable(value = CacheConstants.DICT_DETAILS, key = "#key")
	public R<List<SysDictItem>> getDictByKey(@PathVariable String key) {
		return sysDictService.getDictByKey(key);
	}

}
